package view.common;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;

import base.Session;
import dao.ManagerDao;
import dao.StudentDao;
import dao.UserAccess;
import model.Manager;
import model.Student;
import util.StringUtil;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangepassInterfrm extends JInternalFrame {
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChangepassInterfrm frame = new ChangepassInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChangepassInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("修改密码");
		setBounds(100, 100, 450, 300);
		
		JLabel lblNewLabel = new JLabel("原密码：");
		lblNewLabel.setIcon(new ImageIcon(ChangepassInterfrm.class.getResource("/images/password.png")));
		
		JLabel lblNewLabel_1 = new JLabel("新密码：");
		lblNewLabel_1.setIcon(new ImageIcon(ChangepassInterfrm.class.getResource("/images/password.png")));
		
		passwordField = new JPasswordField();
		
		passwordField_1 = new JPasswordField();
		
		JLabel lblNewLabel_2 = new JLabel("确认密码：");
		lblNewLabel_2.setIcon(new ImageIcon(ChangepassInterfrm.class.getResource("/images/password.png")));
		
		passwordField_2 = new JPasswordField();
		
		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String oldpassword = String.valueOf(passwordField.getPassword());
				String newpassword1 = String.valueOf(passwordField_1.getPassword());
				String newpassword2 = String.valueOf(passwordField_2.getPassword());
			
				if (StringUtil.isEmpty(oldpassword)) {
					JOptionPane.showMessageDialog(null, "原密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField.requestFocus();
					return;
				}
				if (StringUtil.isEmpty(newpassword1)) {
					JOptionPane.showMessageDialog(null, "新密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField_1.requestFocus();
					return;
				}
				if (newpassword1.length()>=20) {
					JOptionPane.showMessageDialog(null, "新密码不能超过20个字符", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField_1.requestFocus();
					passwordField_1.selectAll();
					return;
				}
				if (StringUtil.isEmpty(newpassword2)) {
					JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField_2.requestFocus();
					return;
				}
				if (!newpassword2.equals(newpassword1)) {
					JOptionPane.showMessageDialog(null, "确认密码与原密码不一致", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField_2.requestFocus();
					passwordField_2.selectAll();
					return;
				}
				
				//管理员端
				if ("manager".equals(Session.idStatus)) {
					Manager manager = ManagerDao.getManager(Session.id);
					if (!oldpassword.equals(manager.getT_password())) {
						JOptionPane.showMessageDialog(null, "原密码错误", "提示", JOptionPane.ERROR_MESSAGE);
						passwordField.requestFocus();
						passwordField.selectAll();
						return;
					}
					manager.setT_password(newpassword2);
					if(ManagerDao.updata(manager) > 0) {
						JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					}
				}
				//教师端
				if ("teacher".equals(Session.idStatus)) {
					Manager manager = ManagerDao.getManager(Session.id);
					if (!oldpassword.equals(manager.getT_password())) {
						JOptionPane.showMessageDialog(null, "原密码错误", "提示", JOptionPane.ERROR_MESSAGE);
						passwordField.requestFocus();
						passwordField.selectAll();
						return;
					}
					manager.setT_password(newpassword2);
					if(ManagerDao.updata(manager) > 0) {
						JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					}
				}
				//学生端
				if ("student".equals(Session.idStatus)) {
					Student student = StudentDao.getStudent(Session.id);
					if (!oldpassword.equals(student.getPassword())) {
						JOptionPane.showMessageDialog(null, "原密码错误", "提示", JOptionPane.ERROR_MESSAGE);
						passwordField.requestFocus();
						passwordField.selectAll();
						return;
					}
					student.setPassword(newpassword2);
					if(StudentDao.updata(student) > 0) {
						JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					}
				}
//				dispose();
//				LogOn frame = new LogOn();
//				frame.setVisible(true);
			}
		});
		btnNewButton.setIcon(new ImageIcon(ChangepassInterfrm.class.getResource("/images/confirm.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				passwordField.setText("");
				passwordField_1.setText("");
				passwordField_2.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ChangepassInterfrm.class.getResource("/images/resets.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(99)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addPreferredGap(ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblNewLabel_2)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewLabel_1)
									.addComponent(lblNewLabel)))
							.addGap(30)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(passwordField_1, 137, 137, Short.MAX_VALUE)
								.addComponent(passwordField_2, 137, 137, Short.MAX_VALUE)
								.addComponent(passwordField))))
					.addContainerGap(92, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(50)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(passwordField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(38))
		);
		getContentPane().setLayout(groupLayout);

	}

}
